package tr.gov.tubitak.bilgem.esya.crlviewer;


import sun.security.x509.KeyIdentifier;
import sun.security.x509.X509CRLEntryImpl;
import sun.security.x509.X509CRLImpl;

import java.io.IOException;
import java.math.BigInteger;
import java.security.cert.CRLException;
import java.security.cert.X509CRLEntry;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * Created by IntelliJ IDEA.
 * User: ramazan.girgin
 * Date: 4/17/12
 * Time: 4:19 PM
 * To change this template use File | Settings | File Templates.
 */
public class EViewerCRL {

    X509CRLImpl crlImp;

    public EViewerCRL(X509CRLImpl crlImp) throws CRLException {

        this.crlImp = crlImp;
    }

    List<ERevocationInfo> getRevokedCerts()
    {
        List<ERevocationInfo> retList=new ArrayList<ERevocationInfo>();

        Set<X509CRLEntry> revokedCertificates = crlImp.getRevokedCertificates();
        for (X509CRLEntry revokedCertificate : revokedCertificates) {
            BigInteger serialNumber = revokedCertificate.getSerialNumber();
            String serialNumberStr = EByteTools.toHexString(serialNumber.toByteArray()," ");
            Date revocationDate = revokedCertificate.getRevocationDate();
            Integer reasonCode = null;

            try {
                X509CRLEntryImpl crlEntryImp = new X509CRLEntryImpl(revokedCertificate.getEncoded());
                reasonCode = crlEntryImp.getReasonCode();
            } catch (CRLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            retList.add(new ERevocationInfo(serialNumberStr, revocationDate,reasonCode));

        }
        return retList;
    }

    List<ECrlField> getCrlFields(){
        List<ECrlField> retFieldList = new ArrayList<ECrlField>();

        int version = crlImp.getVersion();
        retFieldList.add(new ECrlField(Bundle.getString("EViewerCRL.field.version"),"v"+version));

        String issuer = crlImp.getIssuerDN().toString();
        retFieldList.add(new ECrlField(Bundle.getString("EViewerCRL.field.issuer"),issuer));
        
        String thisUpdateStr = EDateUtil.date2String(crlImp.getThisUpdate());
        retFieldList.add(new ECrlField(Bundle.getString("EViewerCRL.field.thisUpdate"),thisUpdateStr));

        String nextUpdateStr = EDateUtil.date2String(crlImp.getNextUpdate());
        retFieldList.add(new ECrlField(Bundle.getString("EViewerCRL.field.nextUpdate"),nextUpdateStr));

        String sigAlgName = crlImp.getSigAlgName();
        retFieldList.add(new ECrlField(Bundle.getString("EViewerCRL.field.signatureAlgorithm"),sigAlgName));

        try {
            BigInteger crlNumber = crlImp.getCRLNumber();
            if(crlNumber!=null){
                retFieldList.add(new ECrlField(Bundle.getString("EViewerCRL.field.crlNumber"),EByteTools.toHexString(crlNumber.toByteArray(), " "),false));
            }

            KeyIdentifier authKeyId = crlImp.getAuthKeyId();
            if(authKeyId!=null){
                byte[] identifier = authKeyId.getIdentifier();
                retFieldList.add(new ECrlField(Bundle.getString("EViewerCRL.field.autharithyKeyId"),EByteTools.toHexString(identifier," "),false));
            }
        }catch (IOException e) {
        }
        return retFieldList;
    }

}
